<?php

namespace App\Console\Commands;

use App\Models\AdminUser;
use Illuminate\Console\Command;

class InitAdminUsers extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'users:init';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化后台用户';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $userLists = config('jinbell.users');
        foreach ($userLists as $key => $value) {
            $userModel = AdminUser::where('username',$value['name'])->first();
            if (empty($userModel)) {
                $userModel = new AdminUser();
            }
            $userModel->username = $value['name'];
            $userModel->password = password_hash($value['password'],PASSWORD_DEFAULT);
            $userModel->autho_group = 1;
            $res = $userModel->save();
            if ($res) {
                echo '更新成功：'.$value['name'].PHP_EOL;
            }else{
                echo '更新失败：'.$value['name'].PHP_EOL;
            }
        }
    }
}
